//个人设置页面
var currentPage;
myApp.onPageInit("setting", function(page) {
	currentPage = page;
	console.log("setting init");
	var pageCon = $$(page.container);
	var themeName = localStorage.getItem('themeName') ? localStorage.getItem('themeName') : 'default';
	if (themeName) {
		pageCon.find("[data-theme='" + themeName + "']").addClass('selected');
	}

	pageCon.find(".default-avatar-list .item-content").on("click", function(e) {
		var imageUrl = $(this).find('img').attr('src');
		$(this).addClass("selected").siblings().removeClass("selected");
		convertImgToBase64(imageUrl, function(base64Img) {
			var noTypeBase64Img = base64Img.split(',')[1];
			$$.postJSONP(SERVER_PATH + 'savePersonIcon.do', {
				avatar : noTypeBase64Img
			}, function(json) {
				if (json.code == 1) {
					$$('a.icon-user img').attr('src', base64Img);
					$$('.siderbar .avart img').attr('src', base64Img);
					myApp.alert('头像设置成功！', '提示');
				}
			});
		});
	});

	pageCon.find(".theme-list .item-content").on("click", function(e) {
		var themeName = $(this).data('theme');

		$(this).addClass("selected").siblings().removeClass("selected");
		$('body').attr('data-theme', themeName);

		localStorage.setItem('themeName', themeName);
	});

	pageCon.find(".btn-gallery").on('click', function(e) {
		myApp.confirm('从相册中选择图片?', "提示", function() {
			// 调用壳子相册方法
			TrunPage.callSysAlbum(callSysAlbumCallback);
		});
	});

	pageCon.find(".btn-camera").on('click', function(e) {
		myApp.confirm('立即用手机拍摄照片?', "提示", function() {
			// 调用壳子相机方法
			TrunPage.callSysCamera(callSysCameraCallback);
		});
	});

	/*
	 * pageCon.find(".flat-button").on('click', function(e){
	 * myApp.confirm('保存设置?', "提示"); });
	 */

});

// 头像设置页面
myApp.onPageInit("avatar-preview", function(page) {
	console.log(page.name + " init");
	var pageCon = $$(page.container);
	var avatarInfo = page.query;

	var previewTemplate = Template7.compile('<div class="content-block preview">' + '<img class="base64ImgPreview" src="{{data}}">' + '</div>' + '<div class="content-block">' + '<div class="row">' + '<div class="col-50"><button class="flat-button btn-reelect">重新选取</button></div>' + '<div class="col-50"><button class="flat-button btn-sure">确认</button></div>' + '</div>' + '</div>');
	var html = previewTemplate(avatarInfo);
	pageCon.find('.avatar-preview').html(html);

	pageCon.find('.btn-reelect').on('click', function(e) {
		page.view.back();
	});
	pageCon.find('.btn-sure').on('click', function(e) {
		var imageUrl = avatarInfo.data;
		convertImgToBase64(imageUrl, function(base64Img) {
			var noTypeBase64Img = base64Img.split(',')[1];
			$$.postJSONP(SERVER_PATH + 'savePersonIcon.do', {
				avatar : noTypeBase64Img
			}, function(json) {
				if (json.code == 1) {
					$$('.siderbar .avart img').attr('src', CURRENT_USER.image);
					myApp.alert('头像设置成功！', '提示', function() {
						page.view.back();
					});
				}
			});
		})
	});

});

function callSysAlbumCallback(data) {
	currentPage.view.router.load({
		url : "avatar-preview.html",
		query : {
			data : data
		}
	});
}

function callSysCameraCallback(data) {
	currentPage.view.router.load({
		url : "avatar-preview.html",
		query : {
			data : data
		}
	});
}
